package com.flying.pas.sequence.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.data.Data;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.pas.sequence.model.Sequence;

@Service(value="SequenceService", desc="")
public class SequenceService extends AbstractService{
	
	@MethodInfo("新增")
	@DaoCreate(entity=Sequence.ENTITY_NAME)
	public Sequence create(
			@Param(value=Sequence.CATEGORY, required=true,	desc="CATEGORY") String category,
			@Param(value=Sequence.STEP, required=false,	desc="STEP") Integer step,
			@Param(value=Sequence.VERSION, required=false,	desc="VERSION") Long version,
			@Param(value=Sequence.NEXT_VAL, required=false,	desc="NEXT VAL") Integer next_val,
			@Param(value=Sequence.REMARKS, required=false,	desc="REMARKS") String remarks,
			@Param(value=Sequence.CREATE_DATE, required=false,	desc="CREATE DATE") Date create_date
		){
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Sequence.ENTITY_NAME)
	public ActionResult update(
			@Param(value=Sequence.CATEGORY, required=true,	desc="CATEGORY") String category,
			@Param(value=Sequence.CREATE_DATE, required=false,	desc="CREATE DATE") Date create_date,
			@Param(value=Sequence.NEXT_VAL, required=false,	desc="NEXT VAL") Integer next_val,
			@Param(value=Sequence.REMARKS, required=false,	desc="REMARKS") String remarks,
			@Param(value=Sequence.STEP, required=false,	desc="STEP") Integer step,
			@Param(value=Sequence.UPDATE_DATE, required=false,	desc="UPDATE DATE") Date update_date,
			@Param(value=Sequence.VERSION, required=false,	desc="VERSION") Long version
			
		) throws Exception {
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Sequence.ENTITY_NAME)
	public ActionResult updateNext(
			@Param(value=Sequence.CATEGORY, required=true,	desc="CATEGORY", tag="=") String category,
			@Param(value=Sequence.VERSION, required=false,	desc="VERSION", tag="=") Long version,
			@Param(value=Sequence.NEXT_VAL, required=false,	desc="NEXT VAL") Integer next_val,
			@Param(value=Sequence.UPDATE_DATE, required=false,	desc="UPDATE DATE") Date update_date
		) {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=Sequence.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=Sequence.CATEGORY, required=true,	desc="CATEGORY") String category
		) throws Exception {
		
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Sequence.ENTITY_NAME, pageable=true, modelClass=Sequence.class)
	public QueryResult<Sequence> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Sequence.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Sequence findByCategory(
			@Param(value=Sequence.CATEGORY, required=true,	desc="CATEGORY") String category
		) {
		return null;
	}

	//
	public Data nextValue(
			@Param(value = Sequence.CATEGORY, required=true) String category) {
		Sequence seq = null;
		try {
			seq = this.findByCategory(category);
		} catch (Exception e) {
			seq = this.create(category, 10, 0L, 1, null, new Date());
		}
		seq.setNextVal(seq.getNextVal() + 1);
		this.updateNext(seq.getCategory(), seq.getVersion(), seq.getNextVal(), new Date());
		return new Data("value", seq.getNextVal());
	}
}